<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="Connect.*" %>

<%
	//Retrieve the submitted information.
	String name = request.getParameter("name");
	String year = request.getParameter("year");
	String type = request.getParameter("type");
	String description = request.getParameter("description");

	// No name tests yet.
	boolean name_test = true;
	// Test if year is a non negative four digit number.
	boolean year_test = year.length() == 4;
	try {
		int num = Integer.parseInt(year);
		if (num < 0) {
	year_test = false;
		}
	} catch (Exception e) {
		year_test = false;
	}
	// No type tests yet.
	boolean type_test = true;
	// No description tests yet.
	boolean description_test = true;

	// Compile the error messages together.
	String errors = "";
	if (!name_test) {
		errors += "<br />";
	}
	if (!year_test) {
		errors += "* Year must be a non-negative 4 digit integer.<br />";
	}
	if (!type_test) {
		errors += "<br />";
	}
	if (!description_test) {
		errors += "<br />";
	}

	// If there are any errors abort registration and send the user back to the
	// register page. It also saves non-password type data that the user has submitted.
	if (!errors.equals("")) {
		session.setAttribute("register_errors", errors);
		session.setAttribute("name", name);
		session.setAttribute("year", year);
		session.setAttribute("type", type);
		session.setAttribute("description", description);
		response.sendRedirect("add_item.jsp");
	} else {

		// Getting information and updating the database.
		java.sql.Connection conn = null;
		try {
	// Connect to the database.
	Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
	java.util.Properties sysprops = System.getProperties();
	sysprops.put("user", ConnectionInfo.myUserID());
	sysprops.put("password", ConnectionInfo.myPassword());
	conn = java.sql.DriverManager
	.getConnection(ConnectionInfo.myURL(), sysprops);

	// Check if the requested item exists.
	java.sql.Statement statement = conn.createStatement();
	java.sql.ResultSet rs = statement
	.executeQuery("SELECT name FROM items WHERE name = '"
	+ name
	+ "'"
	+ "AND year='"
	+ year
	+ "'"
	+ "AND type='" + type + "'");
	if (rs.next()) {
		// Item already exists.
		session.setAttribute("register_errors",
		"* An item with the same name, year, and type already exists.");
		session.setAttribute("name", name);
		session.setAttribute("year", year);
		session.setAttribute("type", type);
		session.setAttribute("description", description);
		response.sendRedirect("add_item.jsp");
	} else {
		// The item does not exist in the database yet.
		name = "'" + name + "'";
		year = "'" + year + "'";
		type = "'" + type + "'";
		String id = "'0'";
		String copies_available = "'0'";
		String copies_sold = "'0'";
		description = "'" + description + "'";
		statement
		.executeUpdate("INSERT INTO items (id, name, year, description, type, copies_available, copies_sold) VALUES ("
		+ id
		+ ","
		+ name
		+ ","
		+ year
		+ ","
		+ description
		+ ","
		+ type
		+ ","
		+ copies_available
		+ ","
		+ copies_sold
		+ ")");
	}
		} catch (Exception e) {
	e.printStackTrace();
	out.print(e.toString());
		} finally {
	try {
		conn.close();
	} catch (Exception ee) {
	}
		}
	}
%>
<script>
	onload = function addImage() {
		if (confirm("Do you want to add an image")) {
			window.location = "add_image.jsp";
		} else {
			window.location = "view_items.jsp";
		}
	};
</script>